package com.whf.android.jar.web.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.whf.android.jar.R;
import com.whf.android.jar.app.ConfigKeys;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.base.latte.BaseActivity;
import com.whf.android.jar.web.client.WebChromeClientImpl;
import com.whf.android.jar.web.client.WebLongClickImpl;
import com.whf.android.jar.web.downLoad.DownLoadWebView;
import com.whf.android.jar.web.news.NewsWebView;
import com.whf.android.jar.web.utils.HtmlUtils;

import org.jetbrains.annotations.Nullable;


/**
 * 具体实现的webView的activity
 *
 * @author hf
 * @version 1.0.5
 */
public class WebUiActivity extends BaseActivity {

    private NewsWebView newWebView;

    public static void start(Context context, String fileName, String fileUrl) {
        Intent intent = new Intent();
        intent.setClass(context, WebUiActivity.class);
        intent.putExtra("name", fileName);
        intent.putExtra("data", fileUrl);
        ActivityUtils.startActivity(intent);
    }

    @Override
    protected Object setLayout() {
        return R.layout.hai_activity_web_ui;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String msg = intent.getStringExtra("data");
        //1
        View viewTop = findViewById(R.id.id_view_top);
        if (viewTop != null) {
            addMarginTopEqualStatusBarHeight(viewTop);
        }
        //2
        setOnClickListener(R.id.title_bar_left, v -> finish());
        setTextView(R.id.title_bar_title, TextUtils.isEmpty(name) ? "文件预览" : name);
        //3
        FrameLayout frameLayout = findViewById(R.id.id_layout_frame);
        newWebView = new NewsWebView(getApplicationContext());
        newWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        frameLayout.addView(newWebView);
        newWebView.setWebChromeClient(initWebChromeClient());
        //webView 中的下载事件
        newWebView.setDownloadListener(initDownloadListener());
        //webView 中的长按事件
        newWebView.setOnLongClickListener(initWebLongListener());
        //webView的原生与网页的交互
        final String javascript = Latte.getConfiguration(ConfigKeys.JAVASCRIPT_INTERFACE);
        newWebView.addJavascriptInterface(LatteActivityInterface.create(), javascript);
        if (HtmlUtils.isPdfJs(msg)) {
            newWebView.loadContent("file:///android_asset/pdf/show_pdf.html?" + msg);
        } else {
            newWebView.loadContent(msg);
        }
    }

    public WebChromeClient initWebChromeClient() {
        return new WebChromeClientImpl();
    }

    public DownloadListener initDownloadListener() {
        return new DownLoadWebView(this);
    }

    public WebLongClickImpl initWebLongListener() {
        return new WebLongClickImpl();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (newWebView != null) {
            newWebView.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (newWebView != null) {
            newWebView.onResume();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (newWebView != null) {
            newWebView.clearHistory();
            newWebView.loadDataWithBaseURL(null, "",
                    "text/html", "utf-8", null);
            newWebView.loadUrl("about:blank");
            newWebView.stopLoading();
            newWebView.setWebChromeClient(null);
            newWebView.setWebViewClient(null);
            newWebView.removeAllViews();
            newWebView.destroy();
            newWebView = null;
        }
    }

}
